package com.qeeyou.apps.android.craftsman.activity.login;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.po.CommonInfo;
import com.qeeyou.apps.android.craftsman.service.SystemContext;
import com.qeeyou.apps.android.craftsman.service.UserService;
import com.qeeyou.apps.android.craftsman.view.CustomLoadingDialog;

import java.util.HashMap;
import java.util.Map;

public class ForgetPasswordActivity extends Activity {

    private ImageView btnBack;
    private EditText editUsername;
    private EditText editVerifyCode;
    private Button btnVerifyCode;
    private EditText editPassword;
    private EditText editRetryPassword;
    private Button btnReset;
    private CustomLoadingDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);

        setView();

        setEvent();
    }

    private void setView() {
        btnBack = (ImageView) this.findViewById(R.id.btnBack4ForgetPassword);
        editUsername = (EditText) this.findViewById(R.id.editUsername4ForgetPassword);
        editVerifyCode = (EditText) this.findViewById(R.id.editVerifyCode4ForgetPassword);
        btnVerifyCode = (Button) this.findViewById(R.id.btnVerifyCode4ForgetPassword);
        editPassword = (EditText) this.findViewById(R.id.editPassword4ForgetPassword);
        editRetryPassword = (EditText) this.findViewById(R.id.editRetryPassword4ForgetPassword);
        btnReset = (Button) this.findViewById(R.id.btnReset4ForgetPassword);
    }

    private void setEvent() {

        String username = SystemContext.getInstance().getUsername();
        if(username != null) {
            editUsername.setText(username);
        }

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ForgetPasswordActivity.this.finish();
            }
        });

        btnReset.setOnClickListener(onResetClickListener);

        btnVerifyCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mProgressDialog == null) {
                    mProgressDialog = new CustomLoadingDialog(ForgetPasswordActivity.this);
                }
                mProgressDialog.setMessage("正在获取验证码...");
                mProgressDialog.show();

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if(mProgressDialog != null) {
                            mProgressDialog.dismiss();
                            mProgressDialog = null;
                        }
                        editVerifyCode.setText("123456");
                    }
                }, 10000);
            }
        });

    }

    private View.OnClickListener onResetClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String username = editUsername.getText().toString();
            String password = editPassword.getText().toString();
            String retryPassword = editRetryPassword.getText().toString();
            if(password == null || password.trim().equalsIgnoreCase("")) {
                Toast.makeText(ForgetPasswordActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                return;
            }
            if(retryPassword == null || retryPassword.trim().equalsIgnoreCase("")) {
                Toast.makeText(ForgetPasswordActivity.this, "请再次输入密码", Toast.LENGTH_SHORT).show();
                return;
            }
            if(!password.equalsIgnoreCase(retryPassword)) {
                Toast.makeText(ForgetPasswordActivity.this, "两次输入的密码不一致, 请重新输入", Toast.LENGTH_SHORT).show();
                return;
            }
            username = SystemContext.getInstance().getUsername();
            String oldPassword = SystemContext.getInstance().getPassword();
            String params[] = {oldPassword, password};
            //TODO -- 修改密码
            new ModifyPassword().execute(params);
        }
    };

    //修改密码
    class ModifyPassword extends AsyncTask<String, Void, CommonInfo> {
        public ModifyPassword() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if(mProgressDialog == null) {
                mProgressDialog = new CustomLoadingDialog(ForgetPasswordActivity.this);
            }
            mProgressDialog.setMessage("正在修改密码...");
            mProgressDialog.show();
        }

        @Override
        protected CommonInfo doInBackground(String... params) {
            String oldPassword = params[0];
            String newPassword = params[1];
            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("oldPass", oldPassword);
            paramMap.put("newPass", newPassword);
            CommonInfo resultInfo = UserService.getInstance().userModifyPassword(paramMap);
            return resultInfo;
        }

        @Override
        protected void onPostExecute(CommonInfo resultInfo) {
            super.onPostExecute(resultInfo);
            if(mProgressDialog != null) {
                mProgressDialog.dismiss();
                mProgressDialog = null;
            }
            if(resultInfo != null && resultInfo.getStatus().equalsIgnoreCase("1")) {
                Toast.makeText(ForgetPasswordActivity.this, "修改密码成功", Toast.LENGTH_SHORT).show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ForgetPasswordActivity.this.finish();
                    }
                }, 1500);
            } else {
                Toast.makeText(ForgetPasswordActivity.this, "修改密码失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
